//
//  HotReleasedViewController.m
//  MyMovie
//
//  Created by lanou3g on 15/9/17.
//  Copyright (c) 2015年 HeQiangQiang. All rights reserved.
//

#import "HotReleasedViewController.h"
#import "iCarousel.h"
#import "HotReleased.h"
#import "HotReleasedView.h"
#import "DetailsView.h"
#import "DetailMovieViewController.h"
#import "WebViewController.h"


@interface HotReleasedViewController ()<iCarouselDataSource,iCarouselDelegate>

@property (nonatomic, strong) iCarousel *iCa;
@property (nonatomic, strong) BlurImageView *b;
@property (nonatomic, strong) DetailsView *detailsView;

@property (nonatomic, strong) NSMutableArray *mArray;
@property (nonatomic, strong) NSMutableArray *mBrray;


@end

@implementation HotReleasedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithTitle:@"票房形式" style:(UIBarButtonItemStylePlain) target:self action:@selector(other)];
    self.navigationItem.rightBarButtonItem = rightBar;
   
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
    [SVProgressHUD show];
    [LORequestManger GET:kHotUrl success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *pic in dic[@"subjects"]) {
            HotReleased *model = [HotReleased shareJsonWithDictionary:pic];
            [self.mArray addObject:model];
            [self.mBrray addObject:model.images];
        }
        [self setUp];
        [SVProgressHUD dismiss];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(10, 30, 30, 30);
    button.tag = 999;
    [button addTarget:self action:@selector(button) forControlEvents:(UIControlEventTouchUpInside)];
    [button setBackgroundImage:[UIImage imageNamed:@"user"] forState:(UIControlStateNormal)];
    [self.navigationController.view addSubview:button];
}
//抽屉视图
- (void)button {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:nil];
}
//票房分析
- (void)other {
    UIButton *button = (UIButton *)[self.navigationController.view viewWithTag:999];
    button.hidden = YES;
    WebViewController *view = [[WebViewController alloc] init];
    [self.navigationController pushViewController:view animated:YES];
}
//转动图片
- (void)setUp {
    NSDictionary *dicImages = self.mBrray[0];
    self.b = [[BlurImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 450 * kFitHeight)];
    [self.b sd_setImageWithURL:[NSURL URLWithString:dicImages[@"large"]]];
    
    HotReleasedView *hotView = [[HotReleasedView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 400 * kFitHeight)];
    [hotView addSubview:self.b];
    [self.view addSubview:hotView];
    self.iCa = [[iCarousel alloc] initWithFrame:hotView.bounds];
    self.iCa.type = 4;
    self.iCa.delegate = self;
    self.iCa.dataSource = self;
    self.iCa.pagingEnabled = YES;
    self.iCa.scrollOffset = 0;
    [hotView addSubview:self.iCa];
    self.detailsView= [[DetailsView alloc] initWithFrame:CGRectMake(0, 450 * kFitHeight, kWidth, kHeight - 400 * kFitHeight)];
    [self setDetails:0];
}

- (void)setDetails:(NSInteger)count {
//    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    //设置运动时间
    animation.duration = 0.5;
    animation.delegate = self;
    //设置运动type
    animation.type = @"rippleEffect";
    //设置子类
    animation.subtype = kCATransitionFromBottom;
    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    [self.detailsView.layer addAnimation:animation forKey:@"a"];
    
    HotReleased *hotModel = self.mArray[count];
//    self.detailsView.backgroundColor = [UIColor redColor];
    self.detailsView.title.text = hotModel.title;
    NSDictionary *directors = [hotModel.directors firstObject];
    self.detailsView.directors.text = [@"导演:" stringByAppendingString:directors[@"name"]];
    
    NSString *casts = @"领衔主演:";
    for (NSDictionary *pic in hotModel.casts) {
        casts = [casts stringByAppendingString:pic[@"name"]];
        casts = [casts stringByAppendingString:@" "];
    }
    self.detailsView.casts.text = casts;
    
    NSString *genres = @"类型:\n";
    for (NSString *str in hotModel.genres) {
        genres = [genres stringByAppendingString:str];
        genres = [genres stringByAppendingString:@"\n"];
    }
    self.detailsView.genres.text = genres;
    
    self.detailsView.pubdates.text = [hotModel.pubdates firstObject];
    
    self.detailsView.rating.text = [NSString stringWithFormat:@"评分:\n%.1f",[hotModel.rating[@"average"] floatValue]];
    [self.view addSubview:self.detailsView];
}
- (NSMutableArray *)mArray {
    if (!_mArray) {
        _mArray = [NSMutableArray array];
    }
    return _mArray;
}
- (NSMutableArray *)mBrray {
    if (!_mBrray) {
        _mBrray = [NSMutableArray array];
    }
    return _mBrray;
}
- (NSInteger)numberOfItemsInCarousel:(iCarousel * __nonnull)carousel {
    return self.mArray.count;
}
- (UIView *)carousel:(iCarousel * __nonnull)carousel viewForItemAtIndex:(NSInteger)index reusingView:(nullable UIView *)view {
    
    NSDictionary *dicImages = self.mBrray[index];
    
    if (view == nil) {
        view =[[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 200,  300)];
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 200, 300)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:dicImages[@"large"]]];
    [view addSubview:imageView];
    return view;
}
- (void)carouselDidEndDecelerating:(iCarousel * __nonnull)carousel {
    NSLog(@"%f",self.iCa.scrollOffset);
    if (self.iCa.scrollOffset == -1) {
        self.iCa.scrollOffset = 0;
    }
    if (self.iCa.scrollOffset == self.mArray.count) {
        self.iCa.scrollOffset = self.mArray.count - 1;
    }
    NSDictionary *dic = self.mBrray[(NSInteger)self.iCa.scrollOffset];
    
    [self.b sd_setImageWithURL:[NSURL URLWithString:dic[@"large"]]];
    [self setDetails:self.iCa.scrollOffset];
    
}
//跳转页面
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
    UIButton *button = (UIButton *)[self.navigationController.view viewWithTag:999];
    button.hidden = YES;
    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    //设置运动时间
    animation.duration = 1.0;
    animation.delegate = self;
    //设置运动type
    animation.type = @"pageCurl";
    //设置子类
    animation.subtype = kCATransitionFromBottom;
    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    [self.navigationController.view.layer addAnimation:animation forKey:@"b"];
    
    HotReleased *hotModel = self.mArray[index];
    DetailMovieViewController *view = [[DetailMovieViewController alloc] init];
//    view.movieID = hotModel.ID;
    view.willModel = [[WillReleasedModel alloc] init];
    view.willModel.ID = hotModel.ID;
    view.willModel.title = hotModel.title;
    view.willModel.collection = hotModel.collect_count;
    view.willModel.pubdate = hotModel.mainland_pubdate;
    view.willModel.original_title = hotModel.original_title;
    view.willModel.images = [NSDictionary dictionaryWithDictionary:hotModel.images];
    [self.navigationController pushViewController:view animated:YES];
    
}
- (void)viewWillAppear:(BOOL)animated {
    UIButton *button = (UIButton *)[self.navigationController.view viewWithTag:999];
    button.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
